package com.cn.jdia.springmvc.handlers;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {

	@RequestMapping("/greeting")
	public ModelAndView greeting(@RequestParam(value = "name", defaultValue = "World") String name)
			throws SQLException {
		System.out.println("Hello " + name);

		Map<String, Object> map = new HashMap<String, Object>();
		// map.put("userName", name);
		ClassPathXmlApplicationContext w_context = new ClassPathXmlApplicationContext("springmvc.xml");
		
		DataSource ds = (DataSource) w_context.getBean("dataSource");
		Connection w_con = ds.getConnection();
		Statement w_stmt = null;
		ResultSet w_rs = null;
		try {
			w_con.setAutoCommit(false);
			w_stmt = w_con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
			// w_rs = DBUtil.X61executeQuery(w_stmt, "select * from ta_user
			// where fa_login='dgmislrh' order by fa_login");

			w_rs = w_stmt.executeQuery("select * from ac_event ");

			if (w_rs.next()) {
				map.put("userName", name + "," + w_rs.getString("fa_name"));
			} else {
				map.put("userName", "no found");
			}
		} finally {
			// DBUtil.X63closeConn(w_con, w_stmt, w_rs);
			w_rs.close();
			w_stmt.close();
			w_con.close();
		}
		if (ds != null) {
			return new ModelAndView("/hello", map);
		}
		return null;
	}

	@RequestMapping("/greeting1")
	public ModelAndView greeting1(@RequestParam(value = "name", defaultValue = "World") HttpServletRequest p_request,
			HttpServletResponse p_response, String name) {
		System.out.println("Hello " + name);
		Map<String, Object> map = (Map)p_request.getParameterMap();
		map.put("userName", name);
		return new ModelAndView("/hello", map);
	}
	
}
